//
//  RYVideoToolView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/14.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class RYVideoToolView: SYBaseView {
    
    var playOrPauseClosure: (()->Void)?
    var sliderChangeClosure: ((Float)->Void)?
    
    // 下载进度
    fileprivate var progress: UIProgressView = {
        let pro: UIProgressView = UIProgressView()
        pro.tintColor = .white
        pro.trackTintColor = UIColor(hex: 0x535353)
        return pro
    }()
    
    // 播放进度
    fileprivate var slider: UISlider = {
        let slid: UISlider = UISlider()
        slid.setThumbImage(UIImage(named: "icon_control_dot"), for: .normal)
        slid.addTarget(self, action: #selector(sliderAction), for: .valueChanged)
        slid.maximumTrackTintColor = .clear
        slid.minimumTrackTintColor = UIColor(hex: 0xFFA02A)
        
        return slid
    }()
    
//    fileprivate lazy var playOrPauseBt: UIButton = {
//        let bt: UIButton = UIButton()
//        bt.addTarget(self, action: #selector(playOrPauseAction), for: .touchUpInside)
//        bt.backgroundColor = UIColor(hex: 0xFFA02A)
//        bt.layer.cornerRadius = 25.0.autoWidth()
//        return bt
//    }()
    
    
    fileprivate var currentTimeLb: UILabel = {
        let lb: UILabel = UILabel()
        lb.text = "00:00"
        lb.textColor = .white
        lb.font = 13.autoFontSize()
        lb.textAlignment = .center
        return lb
    }()
    
    fileprivate var totalTimeLb: UILabel = {
        let lb: UILabel = UILabel()
        lb.text = "00:00"
        lb.textColor = .white
        lb.font = 13.autoFontSize()
        lb.textAlignment = .center
        return lb
    }()
    
    let voiceBt: UIButton = {
        let v: UIButton  = UIButton()
        return v
    }()
    
    let quanpingBt: UIButton = {
        let v: UIButton  = UIButton()
        return v
    }()
    
    let voice: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "voice_kai")
        return v
    }()
    
    let quanping: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "quanping_kai")
        return v
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc func playOrPauseAction(sender: UIButton) {
        self.playState(sender: sender)
        if let closure = playOrPauseClosure {
            closure()
        }
    }
    
    @objc func sliderAction(sender: UISlider) {
        if let closure = sliderChangeClosure {
            closure(sender.value)
        }
    }
    
    // 下载进度
    public func setProgress(float: Float) {
        self.progress.setProgress(float, animated: true)
    }
    
    public func setSlider(float: Float) {
        self.slider.setValue(float, animated: true)
        
    }
    
    public func setPlayState(play: Bool) {
//        playOrPauseBt.isSelected = !play
//        self.playState(sender: playOrPauseBt)
    }
    
    public func setCurrentTime(time: CGFloat) {
        self.currentTimeLb.text = Date.secondsToMinSec(second: time)
    }
    
    public func setTotalTime(time: CGFloat) {
        self.totalTimeLb.text = Date.secondsToMinSec(second: time)
    }
    
    fileprivate func playState(sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            sender.setImage(UIImage(named: "zanting"), for: .normal)
        } else {
            sender.setImage(UIImage(named: "bofang"), for: .normal)
        }
    }
    
    override func initViews() {

        voiceBt.addAction {
            
        }
        quanpingBt.addAction {
            
        }
        self.addSubview(self.currentTimeLb)
        self.addSubview(self.totalTimeLb)
        self.addSubview(self.progress)
        self.addSubview(self.slider)
        self.addSubview(self.voice)
        self.addSubview(self.quanping)
        self.addSubview(self.voiceBt)
        
        self.currentTimeLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(38.0.autoWidth())
            make.height.equalTo(12.0.autoWidth())
        }
        
        self.totalTimeLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-38.0.autoWidth())
            make.height.equalTo(12.0.autoWidth())
        }
        
        self.progress.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(96.0.autoWidth())
            make.right.equalToSuperview().offset(-96.0.autoWidth())
            make.height.equalTo(4)
        }
        
        self.slider.snp.makeConstraints { (make) in
            make.centerY.equalTo(progress)
            make.left.equalTo(self.progress.snp.left).offset(-1)
            make.right.equalTo(self.progress.snp.right)
            make.height.equalTo(30)
        }
        self.voice.snp.makeConstraints { (make) in
            make.centerY.equalTo(progress)
            make.left.equalToSuperview().offset(15)
            make.height.width.equalTo(12.autoWidth())
        }
        self.quanping.snp.makeConstraints { (make) in
            make.centerY.equalTo(progress)
            make.right.equalToSuperview().offset(-15)
            make.height.width.equalTo(12.autoWidth())
        }
    }
}


